package co.fitstart.admin.entity.content;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;

import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;

/**
 * 
 * @author wangliwang
 *
 */
public class VisitorRecordSearch implements QueryBuilder {
    
    /** 访客姓名 */
    private String name;
    
    /** 访客电话 */
    private String mobile;
    
    /** 当前用户所在门店 */
    private Site site;
    
    private List<Site> sites;
    
    /** 开始时间 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date begin;
    
    /** 结束时间 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date end;
    
    /** 接待人 */
    private Principal receiver;
    
    private Order order;
    
    private Filter filter;
    
    /**
     * The default constructor
     */
    public VisitorRecordSearch() {
        order = new Order("occurDate", "DESC");
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public Filter getPreparedFilter(boolean refresh) {
        
        if (filter == null || refresh) {
            filter = new Filter().addLike("name", name)
                                 .addLike("mobile", mobile)
                                 .addEquals("site", site)
                                 .addSentence("site IN (:sites)", "sites", sites)
                                 .addEquals("receiver", receiver)
                                 .addDateBetween("occurDate", begin, end);
        }
        
        return filter;
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public Order getOrder() {
        return order;
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        return "FROM VisitorRecord " + getPreparedFilter(false).getWhereSentence() + getOrder().getSentence();
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).getParameters();
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @return the site
     */
    public Site getSite() {
        return site;
    }

    /**
     * @return the sites
     */
    public List<Site> getSites() {
        return sites;
    }

    /**
     * @return the begin
     */
    public Date getBegin() {
        return begin;
    }

    /**
     * @return the end
     */
    public Date getEnd() {
        return end;
    }

    /**
     * @return the receiver
     */
    public Principal getReceiver() {
        return receiver;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @param site the site to set
     */
    public void setSite(Site site) {
        this.site = site;
    }

    /**
     * @param sites the sites to set
     */
    public void setSites(List<Site> sites) {
        this.sites = sites;
    }

    /**
     * @param begin the begin to set
     */
    public void setBegin(Date begin) {
        this.begin = begin;
    }

    /**
     * @param end the end to set
     */
    public void setEnd(Date end) {
        this.end = end;
    }

    /**
     * @param receiver the receiver to set
     */
    public void setReceiver(Principal receiver) {
        this.receiver = receiver;
    }
}
